﻿#region Using directives
using System;
using System.ComponentModel;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for TeamService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class TeamService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public int AddNewTeam(int teamId, int projectId, String teamName) {
            var values = new Object[] {teamId, projectId, teamName};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                var newTeam = new Team {TeamId = teamId, ProjectId = projectId, Name = teamName};
                db.Teams.InsertOnSubmit(newTeam);
                db.SubmitChanges();

                return newTeam.TeamId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }
    }
}